package com.boe.hbd.client.activity;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;

import com.boe.hbd.client.Constants;
import com.boe.hbd.client.R;
import com.boe.hbd.client.contextmenu.IconContextMenu;
import com.boe.hbd.client.contextmenu.MenuBuilder;
import com.boe.hbd.client.contextmenu.IconContextMenu.IconContextItemSelectedListener;
import com.boe.hbd.client.domain.MediaEntity;
import com.boe.hbd.client.persistence.EntityManager;
import com.boe.hbd.client.service.LocationService;
import com.boe.hbd.client.service.ServiceManager;

public class LandscapeHelper {

	public static void buildContextMenu(final Activity activity,final IconContextItemSelectedListener listener,final Object data,final int menuId) {
		Menu menu = new MenuBuilder(activity);
		activity.getMenuInflater().inflate(menuId, menu);
		IconContextMenu cm = new IconContextMenu(activity, menu);
		cm.setInfo(data);
		cm.setOnIconContextItemSelectedListener(listener);
		cm.show();
	}
	
	public static boolean removeEntity(final Activity activity , final Long mediaId) {
		EntityManager entityManager = ServiceManager.getInstance(null).getService(EntityManager.class);
		MediaEntity mediaEntity = entityManager.getById(MediaEntity.class, mediaId);
		if (mediaEntity != null) {
	    	// STEP 1: remove it from the medias
	    	entityManager.remove(mediaEntity);
	    	// STEP 2: remove the media file
	    	Cursor cursor = activity.managedQuery(Uri.parse(mediaEntity.getUri()), new String[] { "_data" }, null, null , null );
	    	if (cursor != null && cursor.moveToFirst()) {
	    		String fileName = cursor.getString(cursor.getColumnIndexOrThrow("_data"));
	    		if (!new File(fileName).delete()) {
	                showFinalAlert(activity,activity.getResources().getText(R.string.delete_failed));
	            }
	    	}
	    	// STEP 3: remove the media DB record
	    	activity.getContentResolver().delete(Uri.parse(mediaEntity.getUri()), null, null);
	    	return true ;
		}
		return false;
	}

	public static void showFinalAlert(final Activity activity , CharSequence message) {
		new AlertDialog.Builder(activity)
				.setTitle(activity.getResources().getText(R.string.alert_title_failure))
				.setMessage(message)
				.setPositiveButton(R.string.alert_ok_button,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,int whichButton) {
								activity.finish();
							}
						}).setCancelable(false).show();
	}
	
	public static void processRequestForLandscape(Activity activity , Long landscapeId, int cmdId) {
		if (cmdId == R.id.takephoto) {
		    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		    activity.startActivityForResult(intent, Constants.ACTIVITY_REQUEST_CODE_CAPTURE_IMAGE);
		} else if (cmdId == R.id.selectphoto) {
		    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		    intent.setType("image/*");
		    activity.startActivityForResult(intent, Constants.ACTIVITY_REQUEST_CODE_CAPTURE_IMAGE);
		} else if (cmdId == R.id.recordvoice) {
		    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		    intent.setType("audio/amr");
		    activity.startActivityForResult(intent, Constants.ACTIVITY_REQUEST_CODE_CAPTURE_VOICE);
		} else if (cmdId == R.id.capturevideo) {
			Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		    activity.startActivityForResult(intent, Constants.ACTIVITY_REQUEST_CODE_CAPTURE_VIDEO);
		} else if (cmdId == R.id.addcomment) {
			Intent intent = new Intent(activity,LandscapeCommentActivity.class);
		    Bundle bundle = new Bundle();
		    bundle.putLong(Constants.KEY_LANDSCAPE_ID, landscapeId);
		    bundle.putInt(Constants.ACTIVITY_REQUEST_CODE, Constants.ACTIVITY_REQUEST_CODE_ADD_COMMENT);
		    intent.putExtras(bundle);
		    activity.startActivityForResult(intent,Constants.ACTIVITY_REQUEST_CODE_ADD_COMMENT);
		} else if (cmdId == R.id.editLandscape) {
			Intent intent = new Intent(activity,LandscapeIntroductionActivity.class);
		    Bundle bundle = new Bundle();
		    bundle.putInt(Constants.ACTIVITY_REQUEST_CODE, Constants.ACTIVITY_REQUEST_CODE_VIEW_LANDSCAPE_DESCRIPTION);
		    bundle.putLong(Constants.KEY_LANDSCAPE_ID, landscapeId);
		    intent.putExtras(bundle);
		    activity.startActivityForResult(intent,Constants.ACTIVITY_REQUEST_CODE_VIEW_LANDSCAPE_DESCRIPTION);
		} else if (cmdId == R.id.comments) {
			Intent intent = new Intent(activity,LandscapeTabActivity.class);
		    Bundle bundle = new Bundle();
		    bundle.putLong(Constants.KEY_LANDSCAPE_ID, landscapeId);
		    bundle.putInt(Constants.ACTIVITY_REQUEST_CODE, Constants.ACTIVITY_REQUEST_CODE_VIEW_LANDSCAPE_COMMENTS);
		    intent.putExtras(bundle);
		    activity.startActivityForResult(intent,Constants.ACTIVITY_REQUEST_CODE_VIEW_LANDSCAPE_COMMENTS);
		} else if (cmdId == R.id.photos) {
			Intent intent = new Intent(activity,LandscapeTabActivity.class);
		    Bundle bundle = new Bundle();
		    bundle.putInt(Constants.ACTIVITY_REQUEST_CODE, Constants.ACTIVITY_REQUEST_CODE_SEE_LANDSCAPE_PHOTOS);
		    bundle.putLong(Constants.KEY_LANDSCAPE_ID,  landscapeId);
		    intent.putExtras(bundle);
		    activity.startActivityForResult(intent,Constants.ACTIVITY_REQUEST_CODE_SEE_LANDSCAPE_PHOTOS);
		} else if (cmdId == R.id.voices) {
			Intent intent = new Intent(activity,LandscapeTabActivity.class);
		    Bundle bundle = new Bundle();
		    bundle.putInt(Constants.ACTIVITY_REQUEST_CODE, Constants.ACTIVITY_REQUEST_CODE_LISTEN_LANDSCAPE_VOICE);
		    bundle.putLong(Constants.KEY_LANDSCAPE_ID,  landscapeId);
		    intent.putExtras(bundle);
		    activity.startActivityForResult(intent,Constants.ACTIVITY_REQUEST_CODE_LISTEN_LANDSCAPE_VOICE);
		} else if (cmdId == R.id.introduction) {
			 Intent intent = new Intent(activity,LandscapeTabActivity.class);
		    Bundle bundle = new Bundle();
		    bundle.putInt(Constants.ACTIVITY_REQUEST_CODE, Constants.ACTIVITY_REQUEST_CODE_VIEW_LANDSCAPE_DESCRIPTION);
		    bundle.putLong(Constants.KEY_LANDSCAPE_ID,  landscapeId);
		    intent.putExtras(bundle);
		    activity.startActivityForResult(intent,Constants.ACTIVITY_REQUEST_CODE_LISTEN_LANDSCAPE_VOICE);
		}
	}

	public static boolean processResultForLandscape(Activity activity,int requestCode, Intent data,Long landscapeId) {
		boolean result = false ;
		Uri uri = data.getData();
		Location location = ServiceManager.getInstance(null).getService(LocationService.class).getLocation();
		String type = null ;
		switch (requestCode) {
		    case Constants.ACTIVITY_REQUEST_CODE_CAPTURE_IMAGE : {
		    	type = Constants.MEDIA_TYPE_IMAGE;
		    }
		    break;
		    case Constants.ACTIVITY_REQUEST_CODE_CAPTURE_VOICE : {
		    	type = Constants.MEDIA_TYPE_VOICE;
		    }
		    break;
		    case Constants.ACTIVITY_REQUEST_CODE_CAPTURE_VIDEO : {
		    	type = Constants.MEDIA_TYPE_VIDEO;
		    }
		    break;
		}
		if (landscapeId != null && uri != null && location != null && type != null ) {
			Cursor cursor = activity.managedQuery(uri, new String[] { "title" }, null, null, null);
			if (cursor != null && cursor.moveToFirst()) {
				String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
				MediaEntity mediaEntity = new MediaEntity(landscapeId,location.getLatitude(),location.getLongitude(),uri.toString(),type,title);
				ServiceManager.getInstance(null).getService(EntityManager.class).create(mediaEntity);
				result = true ;
			}
		}
		return result ;
	}
	
}
